function analysis_template()
% basic template file for data analysis routines.
% gets the current datac window selection, and does the analysis
% for all selected record groups/blocks
% automatically refreshes the data arrays from the disk file if necessary
% and also shows how to generate a figure.
% 8/23/05 P. Manis

% the main routine is just a wrapper to handle the multiple selection and
% catch execution errors.
%
% Note: when testing, set the testflag to 1 so that we DO NOT do try/catch.
% you need to do this to catch the errors in the _go routine.
%
global CONTROL
sf = getmainselection; % read the list of selected traces
testflag = 0;

if(sf > 0)
    pflag = getplotflag; % read teh plot flag (used in the routine template2)

    QueMessage('Template analysis routine', 1); % clear the que
    for i = 1:length(sf)
        if(testflag == 0)
            try
                template_go(sf(i), pflag);
            catch
                fprintf(1, 'Template Analysis Failed execution for sf = %d\n  ***[File: %s, records: %d - %d ]\n', ...
                    sf(i), CONTROL(sf(i)).filename, CONTROL(sf(i)).recbeg, CONTROL(sf(i)).recend);
            end;
        else
            template_go(sf(i), 0); % also turn off plot flag when testing
        end;

    end;
end;



%--------------The real analysis routine------------------

function template_go(sf, pflag)

% access to all of the data and parameters
global DFILE ALLCH VOLTAGE CONTROL

% set some control parameters
isrc = 1;
t0 = 0;
tmax = 1000;
spike_thresh = 0;

% this next routine reads all of the data and information we need,
% including calculating some standard stuff that is put into DPAR.
% you will want DPAR.time especially - it is the time base that goes
% with the data.

[DFILE, DPAR, err] = ana_setup(DFILE, sf);
if(err ~= 0)
    return;
end;

if(~isempty(ALLCH))
    VOLTAGE = ALLCH{isrc};
    CURRENT = ALLCH{isrc+1};
end;


% the analysis here is just finding spikes

[first_spike, first_isi, nr_spikes, splat]=find_spikes2(DFILE, VOLTAGE, t0, tmax, spike_thresh);


% set up a figure window (or you could grab an existing one)
hf = findobj('tag', 'Template'); % find the tagged window with it's name
if(isempty(hf)) % doesn't exist
    hf = figure();
    set(hf, 'tag', 'Template');
    set(hf, 'NumberTitle', 'off');
end;
figure(hf);


clf;% clear it.


% Define a text area and print some standard junk.
subplot('position', [0.1, 0.90, 0.8, 0.095]);
axis([0,1,0,1])
axis('off')
ht(1)=text(0,0.80,sprintf('%-12s R[%d:%d]     %-8s  [%s]',DFILE.filename, DFILE.frec, DFILE.lrec, CONTROL(sf).protocol, date), 'Fontsize', 10);
set(ht(1), 'interpreter', 'none'); % un-TeX the line - this is a filename and won't have tex chars, but might have an underscore.
text(0,0.6,sprintf('Solution:%-12s  gain:%4.1f  LPF:%4.1f kHz', CONTROL(sf).solution, DFILE.igain, DFILE.low_pass(1)), 'FontSize', 8);
text(0,0.4,sprintf('Ihold:%6.2f %s    RMP: %6.2f %s, Rin: %8.3f M\\Omega', ...
    CONTROL(sf).iHold,CONTROL(sf).I_Unit, CONTROL(sf).Rmp, CONTROL(sf).V_Unit, CONTROL(sf).Rin), 'FontSize', 8);


% define some subplots and plot some data

subplot('position', [0.1, 0.075, 0.8, 0.320]);
plot(DPAR.time, ALLCH{2}');
h1 = gca; % get a handle to the axes.
% label the plot and stuff if you need
xlabel('Time (ms)');
ylabel('Voltage (mV)');
box off % turn off the outer right and top axes marks.

subplot('position', [0.1, 0.480, 0.8, 0.320]);
plot(DPAR.time, ALLCH{1}');
h1 = gca;
xlabel('Time (ms)');
ylabel('I (nA)');
box off

if(pflag) % plotflag and enable figure printing
    orient landscape % make the graph landscape on the paper.
    print;
end;



